﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Thuoc24h.Common;
using Thuoc24h.ViewModels;
using Utils = CSS.Utils;
namespace Thuoc24h.Home.Pages
{
    public partial class ProductByCategory : BasePage.BasePages
    {
        public string keyword = "";
        public string categories = "";
        public List<int?> cats = new List<int?>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["categories"] != null)
            {
                categories = Request.QueryString["categories"];
                foreach (var cat in categories.Split(new char[] { ';' }))
                {
                    if (cat != "")
                    {
                        cats.Add(int.Parse(cat));
                    }
                }
            }
            else
            {
                if (Page.RouteData.Values["id"] != null)
                {
                    cats.Add(int.Parse(Page.RouteData.Values["id"].ToString()));
                }
            }       
            if (Request.QueryString["keyword"] != null)
            {
                keyword = Request.QueryString["keyword"];
            }
            Search();
        }
        protected void Search()
        {

            var type = Request.QueryString["type"];

            IQueryable<Data.Product> products = context.Products;
            if (cats.Count > 0)
            {
                products = context.Products.Where(it => cats.Contains(it.CategoryId)).OrderByDescending(it => it.CreatedDate);
                int? cat = cats[0];
                CatNameLbl.Text = context.Categories.FirstOrDefault(it => it.Id == cat).Name;
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = keyword.ToLower();
                products =
                    products.Where(
                        it => it.Name.ToLower().Contains(keyword) || it.UnsignName.ToLower().Contains(keyword));
            }
            if (type != null)
            {
                if (type == "khuyen-mai")
                    products = products.Where(it => it.IsSalesOff == true);
                else if (type == "thuoc-moi")
                {
                    products = products.OrderByDescending(it => it.CreatedDate);
                }
                else if (type == "ban-chay")
                {
                    var children = from p in context.Products
                                   join item in context.OrderDetails on p.Id equals item.ProductId
                                   orderby item.Quantity descending
                                   select p;
                    products = children;
                }
            }
            var productModels = new List<ProductModels>();


            var skip = 0;
            var page = Request.QueryString["page"].ToInt32Return0();
            var pageSize = pagerProduct.PageSize;
            if (page > 0)
            {
                skip = page * pageSize;
            }

            listViewPager.DataSource = products;
            listViewPager.DataBind();

            products = products.OrderBy(item => item.CreatedDate).Skip(skip).Take(pageSize);
            foreach (var product in products)
            {
                var unit = product.Unit;
                var unitPrice = context.PriceUnits.FirstOrDefault(it => it.Unit == unit);
                double? displayPrice = 0;
                double displayDiscountPrice = 0;
                double? displayDiscountPercent = 0;
                if (unitPrice != null)
                {
                    displayDiscountPrice = (double)((unitPrice.Price / 100) * (100 - unitPrice.DiscountPercent));
                    displayPrice = unitPrice.Price;
                    displayDiscountPercent = unitPrice.DiscountPercent;
                }
                else
                {
                    displayDiscountPrice = 0;
                    displayPrice = 0;
                    displayDiscountPercent = 0;
                }
                var productModel = new ProductModels(product.Id, product.Name, product.UnsignName, product.ThumbImage, displayPrice, displayDiscountPrice, displayDiscountPercent, product.DiscountDescription, product.Description);
                productModels.Add(productModel);
            }
            ProductListRpt.DataSource = productModels;
            ProductListRpt.DataBind();

        }
    }
}